package com.aaa.base.controller;


import com.aaa.base.service.LocatorService;
import com.aaa.entity.base.Locator;
import com.aaa.entity.stock.Stock;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/base/locator")
public class LocatorController {

    @Autowired
    private LocatorService locatorService;
    @PostMapping("/searchLocator/{page}/{limit}")
    @ApiOperation(value = "根据条件分页查询")
    public R searchDept(@ApiParam(value = "当前页数") @PathVariable Integer page,
                        @ApiParam(value = "每页个数") @PathVariable Integer limit,
                        @ApiParam(value = "查询条件") @RequestBody Locator locator
                        ){
        Page<Locator> locatorPage = locatorService.searchLocator(page, limit, locator,41L);
        return R.ok().message("分页查询成功").code(200).data("locatorPage",locatorPage);
    }

    @PostMapping("/saveOrupLocator")
    @ApiOperation(value = "新增或修改方法，有id为修改")
    public R addSupplier(@RequestBody Locator locator){
        if(locator.getId()!=null){
            List<Stock> stocks= locatorService.selectStockByLocCode(locator.getCode());
            if (stocks.size()>0){
                return R.error().message("该货位尚有库存,不能修改为失效，请核实！");
            }
        }
        boolean b = locatorService.saveOrUpdate(locator);
        if (b) {
            return R.ok().message("操作成功").code(200);
        }else {
            return R.error().message("操作失败").code(500);
        }
    }



    @PostMapping("/delLocator/{id}")
    @ApiOperation(value = "根据ID删除")
    public R delSupplier(@PathVariable Long id){
        boolean b = locatorService.removeById(id);
        if (b) {
            return R.ok().message("删除成功").code(200);
        }else {
            return R.error().message("删除失败").code(500);
        }
    }

    @PostMapping("/findType")
    @ApiOperation(value = "遍历type类型")
    public R findType(){
        List<Map> type = locatorService.findType();
        return R.ok().message("查询成功").code(200).data("typeinfo",type);

    }
    /*通过货位编码和仓库id查询目标货位*/
    @GetMapping("selectTarLocByLoCode")
    @ApiOperation(value = "通过货位编码和仓库id查询目标货位")
    public Locator selectTarLocByLoCode(@RequestParam String locatorcode,@RequestParam Long warehouseid){
        return locatorService.selectTarLocByLoCode(locatorcode,warehouseid);
    }

    /*通过货位id查询目标货位*/
    @GetMapping("selectLocById")
    @ApiOperation(value = "通过货位id查询目标货位")
    public Locator selectLocById(@RequestParam Long id){
        return locatorService.getById(id);
    }

    /*通过仓库id查询所有货位*/
    @GetMapping("selectLocByWareId")
    @ApiOperation(value = "通过仓库id查询所有货位")
    public List<Locator> selectLocByWareId(@RequestParam Long warehouseid){
        return locatorService.selectLocByWareId(warehouseid);
    }

    /*通过仓库id查询所有入库区货位*/
    @GetMapping("selectInLocByWareId")
    @ApiOperation(value = "通过仓库id查询所有入库区货位")
    public List<Locator> selectLocByWareIdState(@RequestParam Long warehouseid){
        return locatorService.selectLocByWareIdState(warehouseid);
    }

    /*通过仓库id查询所有货位*/
    @PostMapping("/selectLoc/{warehouseid}")
    @ApiOperation(value = "通过仓库id查询所有货位")
    public List<Locator> selectByWareId(@PathVariable Long warehouseid){
        return locatorService.selectLocByWareId(warehouseid);
    }
    /*查询除默认仓库的所有货位*/
    @PostMapping("selectIMoLo")
    public List<Map> selectIMoLo(){
        return locatorService.selectIMoLo();
    }
}

